﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

namespace YJKArchiFormat
{
	public class ConfigUtils
	{
		public static string GetStandardRoomConfigFileName()
		{
			string location = Assembly.GetExecutingAssembly().Location;
			int length = location.LastIndexOf("\\");
			string text = location.Substring(0, length);
			int length2 = text.LastIndexOf("\\");
			string text2 = text.Substring(0, length2) + "\\Data" + "\\StandardRoomConfig.xml";
			if (!new FileInfo(text2).Exists)
			{
				ConfigUtils.CreateStandardRoomXmlFile(text2);
			}
			return text2;
		}

		public static void CreateStandardRoomXmlFile(string fileName)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
				xmlDocument.AppendChild(newChild);
				XmlElement xmlElement = xmlDocument.CreateElement("", "标准间配置", "");
				XmlElement xmlElement2 = xmlDocument.CreateElement("标准间");
				XmlElement xmlElement3 = xmlDocument.CreateElement("数据库类型");
				xmlElement3.SetAttribute("数据库类型", "Local");
				XmlElement xmlElement4 = xmlDocument.CreateElement("本地数据");
				xmlElement4.SetAttribute("数据库路径", "");
				XmlElement xmlElement5 = xmlDocument.CreateElement("服务器数据");
				xmlElement5.SetAttribute("服务器地址", "");
				xmlElement5.SetAttribute("用户名", "");
				xmlElement5.SetAttribute("密码", "");
				xmlElement2.AppendChild(xmlElement3);
				xmlElement2.AppendChild(xmlElement4);
				xmlElement2.AppendChild(xmlElement5);
				xmlElement.AppendChild(xmlElement2);
				xmlDocument.AppendChild(xmlElement);
				xmlDocument.Save(fileName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public static DbData ReadStandardRoomXmlFile(string DbType)
		{
			string standardRoomConfigFileName = ConfigUtils.GetStandardRoomConfigFileName();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(standardRoomConfigFileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("标准间配置");
			XmlNode xmlNode2 = null;
			foreach (object obj in xmlNode.ChildNodes)
			{
				XmlNode xmlNode3 = (XmlNode)obj;
				if (xmlNode3.Name.Trim().ToLower().Equals("标准间"))
				{
					xmlNode2 = xmlNode3;
				}
			}
			if (!(DbType == "数据库类型"))
			{
				return null;
			}
			string text = xmlNode2.ChildNodes[0].Attributes[DbType].Value.Trim();
			if (text.Equals("Local"))
			{
				return new LocalDbData(xmlNode2.ChildNodes[1].Attributes["数据库路径"].Value);
			}
			if (text.Equals("Server"))
			{
				XmlNode xmlNode4 = xmlNode2.ChildNodes[2];
				return new ServerDbData(xmlNode4.Attributes["服务器地址"].Value, xmlNode4.Attributes["用户名"].Value, xmlNode4.Attributes["密码"].Value);
			}
			return null;
		}

		public static void WriteStandardRoomXmlFile(string fileName, DbData data)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(fileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("标准间配置");
			XmlNode xmlNode2 = null;
			foreach (object obj in xmlNode.ChildNodes)
			{
				XmlNode xmlNode3 = (XmlNode)obj;
				if (xmlNode3.Name.Trim().ToLower().Equals("标准间"))
				{
					xmlNode2 = xmlNode3;
				}
			}
			xmlNode2.ChildNodes[0].Attributes["数据库类型"].Value = data.DatabaseType.ToString();
			if (data is LocalDbData)
			{
				xmlNode2.ChildNodes[1].Attributes["数据库路径"].Value = ((LocalDbData)data).DatabasePath;
			}
			else if (data is ServerDbData)
			{
				XmlNode xmlNode4 = xmlNode2.ChildNodes[2];
				xmlNode4.Attributes["服务器地址"].Value = ((ServerDbData)data).ConnectData.IP;
				xmlNode4.Attributes["用户名"].Value = ((ServerDbData)data).ConnectData.UserName;
				xmlNode4.Attributes["密码"].Value = ((ServerDbData)data).ConnectData.Password;
			}
			xmlDocument.Save(fileName);
		}
	}
}
